var mongoose = require('mongoose');

const get = async ({page,limit,sort,data}) => {
    const result = {};
    const storeGoodsModel = mongoose.model("storeGoods");
    result.total = await storeGoodsModel.find(data);
    result.total = result.total.length;
    result.row = await storeGoodsModel
        .find(data)
        .skip((page - 1) * limit) //跳过
        .limit(limit) //限制
        .sort({
            _id: sort=="+1"?1:-1
        }) //排序
        .exec();
    result.maxPage = Math.ceil(result.total / limit);
    return result;
}

const create = async (data) => await mongoose.model("storeGoods").create(data);

const update = async (data) => {
    const {_id} = data;
    await mongoose.model("storeGoods").updateOne({
        _id
    }, data)
};

const remove = async (_id) => await mongoose.model("storeGoods").deleteOne({
    _id
});

//服务
const createServe = async (data) => await mongoose.model("service").create(data);

const getServe = async ({page,limit,sort,data}) => {
    const result = {};
    const serviceModel = mongoose.model("service");
    result.total = await serviceModel.find(data);
    result.total = result.total.length;
    result.row = await serviceModel
        .find(data)
        .skip((page - 1) * limit) //跳过
        .limit(limit) //限制
        .sort({
            _id: sort=="+1"?1:-1
        }) //排序
        .exec();
    result.maxPage = Math.ceil(result.total / limit);
    return result;
}

const updateServe = async (data) => {
    const {_id} = data;
    await mongoose.model("service").updateOne({
        _id
    }, data)
};

const removeServe = async (_id) => await mongoose.model("service").deleteOne({
    _id
});

module.exports = {
    get,
    create,
    update,
    remove,
    createServe,
    getServe,
    updateServe,
    removeServe
}